package.cpath = package.cpath .. ';C:/Users/win/AppData/Roaming/JetBrains/IdeaIC2020.3/plugins/intellij-emmylua/classes/debugger/emmy/windows/x86/?.dll'
local dbg = require('emmy_core')
dbg.tcpConnect('localhost', 9966)

package.path = package.path .. ";C:/Users/win/Documents/_lua/util/?.lua"

local Deque = require("Deque")

function Test1(capacity)
    local dq = Deque.new(capacity)

    local EmptyCheck = function()
        assert(nil == dq:PeekFirst())
        assert(nil == dq:PeekLast())
        assert(0 == dq:GetCount())
        assert("" == tostring(dq))
    end
    EmptyCheck()

    --AddLast+RemoveLast
    local AddLastCheck = function()
        dq:AddLast(1)
        assert(1 == dq:GetCount())
        assert(1 == dq:PeekFirst())
        assert(1 == dq:PeekLast())
        assert("1" == tostring(dq))

        dq:AddLast(2)
        assert(2 == dq:GetCount())
        assert(1 == dq:PeekFirst())
        assert(2 == dq:PeekLast())
        assert("1,2" == tostring(dq))

        dq:AddLast(3)
        assert(3 == dq:GetCount())
        assert(1 == dq:PeekFirst())
        assert(3 == dq:PeekLast())
        assert("1,2,3" == tostring(dq))

        dq:AddLast(4)
        assert(4 == dq:GetCount())
        assert(1 == dq:PeekFirst())
        assert(4 == dq:PeekLast())
        assert("1,2,3,4" == tostring(dq))

        dq:AddLast(5)
        assert(5 == dq:GetCount())
        assert(1 == dq:PeekFirst())
        assert(5 == dq:PeekLast())
        assert("1,2,3,4,5" == tostring(dq))
    end
    AddLastCheck()

    dq:Clear()
    EmptyCheck()

    AddLastCheck()

    local RemoveLastCheck = function()
        assert(5 == dq:RemoveLast())
        assert(4 == dq:GetCount())
        assert(1 == dq:PeekFirst())
        assert(4 == dq:PeekLast())
        assert("1,2,3,4" == tostring(dq))

        assert(4 == dq:RemoveLast())
        assert(3 == dq:GetCount())
        assert(1 == dq:PeekFirst())
        assert(3 == dq:PeekLast())
        assert("1,2,3" == tostring(dq))

        assert(3 == dq:RemoveLast())
        assert(2 == dq:GetCount())
        assert(1 == dq:PeekFirst())
        assert(2 == dq:PeekLast())
        assert("1,2" == tostring(dq))

        assert(2 == dq:RemoveLast())
        assert(1 == dq:GetCount())
        assert(1 == dq:PeekFirst())
        assert(1 == dq:PeekLast())
        assert("1" == tostring(dq))

        assert(1 == dq:RemoveLast())
        assert(0 == dq:GetCount())
        assert(nil == dq:PeekFirst())
        assert(nil == dq:PeekLast())
        assert("" == tostring(dq))
    end
    RemoveLastCheck()

    --AddFirst+RemoveLast
    local AddFirstCheck = function()
        dq:AddFirst(5)
        assert(1 == dq:GetCount())
        assert(5 == dq:PeekFirst())
        assert(5 == dq:PeekLast())
        assert("5" == tostring(dq))

        dq:AddFirst(4)
        assert(2 == dq:GetCount())
        assert(4 == dq:PeekFirst())
        assert(5 == dq:PeekLast())
        assert("4,5" == tostring(dq))

        dq:AddFirst(3)
        assert(3 == dq:GetCount())
        assert(3 == dq:PeekFirst())
        assert(5 == dq:PeekLast())
        assert("3,4,5" == tostring(dq))

        dq:AddFirst(2)
        assert(4 == dq:GetCount())
        assert(2 == dq:PeekFirst())
        assert(5 == dq:PeekLast())
        assert("2,3,4,5" == tostring(dq))

        dq:AddFirst(1)
        assert(5 == dq:GetCount())
        assert(1 == dq:PeekFirst())
        assert(5 == dq:PeekLast())
        assert("1,2,3,4,5" == tostring(dq))
    end
    AddFirstCheck()
    RemoveLastCheck()

    --AddFirst+RemoveFirst
    local RemoveFirstCheck = function()
        assert(1 == dq:RemoveFirst())
        assert(4 == dq:GetCount())
        assert(2 == dq:PeekFirst())
        assert(5 == dq:PeekLast())
        assert("2,3,4,5" == tostring(dq))

        assert(2 == dq:RemoveFirst())
        assert(3 == dq:GetCount())
        assert(3 == dq:PeekFirst())
        assert(5 == dq:PeekLast())
        assert("3,4,5" == tostring(dq))

        assert(3 == dq:RemoveFirst())
        assert(2 == dq:GetCount())
        assert(4 == dq:PeekFirst())
        assert(5 == dq:PeekLast())
        assert("4,5" == tostring(dq))

        assert(4 == dq:RemoveFirst())
        assert(1 == dq:GetCount())
        assert(5 == dq:PeekFirst())
        assert(5 == dq:PeekLast())
        assert("5" == tostring(dq))

        assert(5 == dq:RemoveFirst())
        assert(0 == dq:GetCount())
        assert(nil == dq:PeekFirst())
        assert(nil == dq:PeekLast())
        assert("" == tostring(dq))
    end
    AddFirstCheck()
    RemoveFirstCheck()

    --AddLast+RemoveFirst
    AddLastCheck()
    RemoveFirstCheck()
end
Test1(8) --不触发扩容
Test1(3) --触发扩容


function Test2()
    local dq = Deque.new(5)

    dq:AddFirst(4) -- ____4
    dq:AddFirst(3)
    dq:AddFirst(2)
    dq:AddFirst(1) --_1234

    dq:AddLast(5) --51234, 触发扩容和头尾相掉
    assert(5 == dq:GetCount())
    assert(1 == dq:PeekFirst())
    assert(5 == dq:PeekLast())
    assert("1,2,3,4,5" == tostring(dq))
end
Test2()
